﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KZ400B
{
    public class AxleToZeroManageV1
    {
        bool registed = false;
        public void Regist(string aName)
        {
            if (!string.IsNullOrEmpty(aName))
            {
                List<string> aList = new List<string>();
                aList.Add(aName);
                Regist(aList);
            }

        }

        public void Regist(Dictionary<string, Axle> axleDict)
        {
            if (axleDict != null && axleDict.Count > 0)
            {
                List<string> aList = new List<string>();
                foreach (KeyValuePair<string, Axle> a in axleDict)
                {
                    aList.Add(a.Key);
                }
                Regist(aList);
            }
        }

        public void Regist(List<string> axleList)
        {
            if (axleList != null)
            {
                lock (s_rgLock)
                {
                    if (!registed)
                    {
                        AxleManager.OnAxleActionResponse += new AxleActionResponse(AxleManager_OnAxleActionResponse);
                    }
                    registed = true;
                }
                lock (s_tzLock)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                foreach (string a in axleList)
                {
                    lock (s_tzLock)
                    {
                        s_tzList.Add(a,false);
                    }
                }
            }
        }

        void AxleManager_OnAxleActionResponse(
            string axleName
            , Axle axle
            , AxleActions action
            , bool success
            , string message)
        {
            if (action == AxleActions.ToZeroFinish)
            {
                setToZeroAxleReady(axleName,success);
            }
        }

        void setToZeroAxleReady(string aName,bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList != null && s_tzList.Count > 0 && s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
            }
        }

        
        public void UnRegist()
        {
            lock (s_rgLock)
            {
                AxleManager.OnAxleActionResponse -= new AxleActionResponse(AxleManager_OnAxleActionResponse);
                registed = false;
            }
        }
         object s_rgLock = new object();
         object s_tzLock = new object();
         Dictionary<string,bool> s_tzList;

        public  bool IsToZeroReady(string aName)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null || s_tzList.Count == 0) return false;
                if (s_tzList.ContainsKey(aName) && s_tzList[aName])
                {
                    return true;
                }
            }

            return false;
        }

        public void ResetAction(string aName)
        {
            ResetAction(aName, false);
        }
        public void ResetAction(string aName, bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                if (s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
                else
                {
                    s_tzList.Add(aName, ready);
                }
            }
        }
        
        
    }


    public class AxleToZeroManageV2
    {
        bool registed = false;
        public void Regist(string aName)
        {
            if (!string.IsNullOrEmpty(aName))
            {
                List<string> aList = new List<string>();
                aList.Add(aName);
                Regist(aList);
            }

        }

        public void Regist(Dictionary<string, Axle> axleDict)
        {
            if (axleDict != null && axleDict.Count > 0)
            {
                List<string> aList = new List<string>();
                foreach (KeyValuePair<string, Axle> a in axleDict)
                {
                    aList.Add(a.Key);
                }
                Regist(aList);
            }
        }

        public void Regist(List<string> axleList)
        {
            if (axleList != null)
            {
                lock (s_rgLock)
                {
                    if (!registed)
                    {
                        //AxleManager.OnAxleActionResponse += new AxleActionResponse(AxleManager_OnAxleActionResponse);
                    }
                    registed = true;
                }
                lock (s_tzLock)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                foreach (string a in axleList)
                {
                    lock (s_tzLock)
                    {
                        s_tzList.Add(a, false);
                    }
                }
            }
        }

        void AxleManager_OnAxleActionResponse(
            string axleName
            , Axle axle
            , AxleActions action
            , bool success
            , string message)
        {
            if (action == AxleActions.ToZeroFinish)
            {
                setToZeroAxleReady(axleName, success);
            }
        }

        void setToZeroAxleReady(string aName, bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList != null && s_tzList.Count > 0 && s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
            }
        }


        public void UnRegist()
        {
            lock (s_rgLock)
            {
                AxleManager.OnAxleActionResponse -= new AxleActionResponse(AxleManager_OnAxleActionResponse);
                registed = false;
            }
        }
        object s_rgLock = new object();
        object s_tzLock = new object();
        Dictionary<string, bool> s_tzList;

        public bool IsToZeroError(string aName)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null || s_tzList.Count == 0) return false;
                //if (s_tzList.ContainsKey(aName) && s_tzList[aName])
                //{
                //    return true;
                //}
                else
                {
                    bool rst = true;
                    foreach (string an in s_tzList.Keys)
                    {
                        if (an != aName) continue;
                        if (AxleCacheManager.AxleList.ContainsKey(an))
                        {
                            rst &= AxleCacheManager.AxleList[an].IsToZeroed;
                            if (!rst  && 
                                AxleCacheManager.AxleList[an].AxleStatus == AxleStatus.Stoped
                                && AxleCacheManager.AxleList[an].OtherAxleStatus >= AxleActions.ToZeroError
                                 
                                )
                            {
                                return true;
                            }
                        }
                    }

                }
            }
            return false;
        }

        public bool IsToZeroReady(string aName)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null || s_tzList.Count == 0) return true;
                //if (s_tzList.ContainsKey(aName) && s_tzList[aName])
                //{
                //    return true;
                //}
                else
                {
                    //bool rst = true;
                    foreach (string an in s_tzList.Keys)
                    {
                        if (an != aName) continue;
                        if (AxleCacheManager.AxleList.ContainsKey(an))
                        {
                            return AxleCacheManager.AxleList[an].IsToZeroed;
                            
                        }
                    }

                    //bool rst = true;
                    //foreach (string an in s_tzList.Keys)
                    //{
                    //    if (AxleCacheManager.AxleList.ContainsKey(an))
                    //    {
                    //        rst &= (AxleCacheManager.AxleList[an].AxleStatus == AxleStatus.Stoped
                    //            && AxleCacheManager.AxleList[an].OtherAxleStatus != AxleActions.ToZeroError
                    //            )
                    //                || AxleCacheManager.AxleList[an].OtherAxleStatus == AxleActions.ToZeroFinish
                    //            ;
                    //        if (!rst) return false;
                    //    }
                    //}
                    
                }
            }

            return false;
        }

        public void ResetAction(string aName)
        {
            ResetAction(aName, false);
        }
        public void ResetAction(string aName, bool ready)
        {
            lock (s_tzLock)
            {
                if (s_tzList == null)
                {
                    s_tzList = new Dictionary<string, bool>();
                }
                if (s_tzList.ContainsKey(aName))
                {
                    s_tzList[aName] = ready;
                }
                else
                {
                    s_tzList.Add(aName, ready);
                }
            }
        }


    }
}
